<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Send Message</title>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link href="style3.css" rel="stylesheet" type="text/css" />
        <link href="jquery-ui-all.css" rel="stylesheet" type="text/css" />
    </head>
    <?php
    if (isset($_SESSION['lang']))
        require_once "./language_files/choosepeople_" . $_SESSION['lang'] . ".php";
    else
        require_once "./language_files/choosepeople_en.php";
    $member_id = $_SESSION['member_id'];
    include 'functions.php';
    echo "<fieldset>";
    //creating the drop down lis
    ?><legend><?php echo $lang['title_1']; ?></legend><?php
    //query to retrieve all the oncferences where the user is the creator
    $sql = ("SELECT DISTINCT c.conference_id, c.long_name FROM conference c
            INNER JOIN member_privileges m ON (m.conference_id = c.conference_id)
            WHERE (c.creator_id='$member_id') OR (m.member_id = '$member_id') AND (privileges_id = '2')");
    $result = mysql_query($sql) or die(mysql_error());
    $num_rows = mysql_num_rows($result);
    if ($num_rows > 0) {
        echo "
    <p align='center'><strong>Conference:</strong> <select name='conference' id='Cid'>";
        echo '<option value="please select a conference" selected="selected">please select a conference</option>';
        for ($i = 0; $i < $num_rows; $i++) {
            $confID = mysql_result($result, $i, 0);
            $s_name = mysql_result($result, $i, 1);
            $short_name = formatText_safe($s_name);
            echo '<option value="' . $confID . '">' . $short_name . '</option>';
        }
        echo "</select></p>
  ";
        echo "<form name='fRadio' method='POST' action='send.php'>";
        echo "<div id='accordion'>";
             echo '<h3><a href="#">Choose Authors</a></h3>';
             print "<div id='authorDiv'><p align='center'>Choose conference first!</p></div>";
             echo '<h3><a href="#">Choose Reviewers</a></h3><div id="reviewerDiv">
                  <p align="center">Choose conference first!</p></div>
                </div>';
    echo '<div id="container">';
    $sql = ("SELECT template_id, template_text, template_name, subject_text, member_id FROM message_templates
        WHERE member_id = {$member_id}");
    $result = mysql_query($sql) or die(mysql_error());
    $num_rows = mysql_num_rows($result);
    $confName = get_conference_name($confID);
    $confName = formatText_safe($confName);
    echo "<label>" . $lang['my_template'] . "</label><select id='tempchoose' name='templates'>";
    if ($num_rows == 0) {
        echo '<option value=""> ' . $lang['no_saved'] . '</option>';
    } else {
        for ($i = 0; $i < $num_rows; $i++) {
            //$template_id = mysql_result($result, $i, 0);
            $temp_text = mysql_result($result, $i, 1);
            $temp_name = mysql_result($result, $i, 2);
            $subject_text = mysql_result($result, $i, 3);
            echo '<option subject="' . $subject_text . '" value="' . $temp_text . '">' . $temp_name . '</option>';
        }
    }
    echo "</select></div>";
        ?>
        <input confName="<?php echo $confName; ?>" id="thankyou" name="dOption" value="N" onClick="activate()" type="radio"/> <label class="thankyou" for="thankyou"><?php echo $lang['template_1']; ?></label>
        <input confName="<?php echo $confName; ?>" id="reminder" name="dOption" value="T" onClick="activate()" type="radio"/> <label class="thankyou" for="reminder"><?php echo $lang['template_2']; ?> </label>
        <input id="empty" name="dOption" value="T" onClick="activate()" type="radio"> <label class="thankyou" for="empty"/><?php echo $lang['template_3']; ?></label><br/>
    <?
    mysql_close();
    ?>
    <!--functions to activate the text box-->
    <br /><label align="left">Subject:</label><br /><input id="subject" name="subject" value="" type="text"/>
    <br /><label align="left">Message:</label><textarea style="width: 99%;" id="textarea" name="TextBox" rows="8" disabled="disabled" type="text" value=""/></textarea><br/>
<button class="disabledButton" id="send" name="Message" value="" disabled="disabled" 
        type="submit"><?php echo $lang['button_name']; ?></button>
    <input id="cc" type="hidden" name="conference" value="<?php echo $confID ?>"/>
    <input id="temp" name="temp" type="button" value="<?php echo $lang['button']; ?>" />
    </fieldset>
    </form>
    <?php
} else {
    ?> <p align='center'> You are not organizer of any conference to send messages.</p>
    <?php
}
?>
</body>
<?php include "jquery.php"; ?>
<script type="text/javascript" src="functions.js"></script>
<script type="text/javascript" src="jquery-accordion.js"></script>
<script type="text/javascript">
    function activate()
    {
        if ($('#empty').is(':checked')) {
            if($('#textarea').val() == '') {
                $("#textarea").removeAttr("disabled");
                $("#textarea").val("");
                $("#send").removeAttr("disabled");
                $("#send").removeClass("disabledButton");

            }
            else {
                var alrt = confirm("<?php echo $lang['warning_1']; ?> ");
                if(alrt == true) {
                    $("#textarea").removeAttr("disabled");
                    $("#textarea").val("");
                    $("#send").removeAttr("disabled");
                    $("#send").removeClass("disabledButton");
                }
            }
        }
        else if($('#thankyou').is(':checked')) {

            if($('#textarea').val() == '') {
                conf = $("#thankyou").attr("confName");
                $("#textarea").removeAttr("disabled");
                $("#textarea").val("<?php echo $lang['warning_2']; ?>" +' ' + conf +' '+"<?php echo $lang['warning_3']; ?>");
                $("#send").removeAttr("disabled");
                $("#send").removeClass("disabledButton");

            }
            else {
                var alrt = confirm("<?php echo $lang['warning_1']; ?>");
                if(alrt == true) {
                    conf = $("#thankyou").attr("confName");
                    $("#textarea").removeAttr("disabled");
                    $("#textarea").val("<?php echo $lang['warning_2']; ?>"+ ' ' +conf +' '+"<?php echo $lang['warning_3']; ?>");
                    $("#send").removeAttr("disabled");
                    $("#send").removeClass("disabledButton");
                }
            }
        }
        else if($('#reminder').is(':checked')) {

            if($('#textarea').val() == '') {
                conf = $("#reminder").attr("confName");
                $("#textarea").removeAttr("disabled");
                $("#textarea").val("<?php echo $lang['warning_2']; ?>"+ ' ' + conf +' '+"<?php echo $lang['warning_4']; ?>");
                $("#send").removeAttr("disabled");
                $("#send").removeClass("disabledButton");
            }
            else {
                var alrt = confirm("<?php echo $lang['warning_1']; ?>");
                if(alrt == true) {
                    conf = $("#reminder").attr("confName");
                    $("#textarea").removeAttr("disabled");
                    $("#textarea").val("<?php echo $lang['warning_2']; ?>" +' ' +conf+' '+ "<?php echo $lang['warning_4']; ?>");
                    $("#send").removeAttr("disabled");
                    $("#send").removeClass("disabledButton");
                }
            }
        }
    }
    //creating the radio button list and the text box
</script>
<script>
    //this part of the code is for hiding and showing the tables of authors and reviewers on clicking them
    $(document).ready(function(){
        check_top();
        $(function() {
            $("#accordion").accordion({autoHeight:false, collapsible: true, navigation: true,
                        clearStyle: true, 
                        change: function(event, ui) {
                            resize_iframe();
                        }
                    });
        });
        $("#Cid").bind("change", function() {
            var intRegex = /^\d+$/;
            confID = $(this).val();
            if(intRegex.test(confID)) {
            $("#cc").val(confID);
            $.ajax ({
                data: {confID: confID},
                type: 'POST',
                url:  'conferenceAuthors.php',
                success: function(response) {
                    $("#authorDiv").html(response);  
                    resize_iframe();
                }
            });
            $.ajax ({
                data: {confID: confID},
                type: 'POST',
                url:  'conferenceReviewers.php',
                success: function(response) {
                    $("#reviewerDiv").html(response);  
                    resize_iframe();
                }
            });
            } else {
              $("#authorDiv").html('<p align="center">Choose conference first!</p>');  
               $("#reviewerDiv").html('<p align="center">Choose conference first!</p>');  
            }
        });
        $(document).on("click","#Aselectall", function(){
            if($("#Authors input[type=checkbox]").attr("checked")) {
                $("#Authors input[type=checkbox]").attr('checked',true);
                $("#Authors label").html("Un-check all");
            } else {
                $("#Authors input[type=checkbox]").attr('checked',false);
                $("#Authors label").html("Check all");
            }
        });
         $(document).on("click","#Rselectall", function(){
            if($("#Reviewers input[type=checkbox]").attr("checked")) {
                $("#Reviewers input[type=checkbox]").attr('checked',true);
                $("#Reviewers label").html("Un-check all");
            } else {
                $("#Reviewers input[type=checkbox]").attr('checked',false);
                $("#Reviewers label").html("Check all");
            }
        });

        $(document).on("click","#temp", function(){
            name=prompt("template name?");
            temp_text = $("#textarea").val();
            subject_text = $("#subject").val();
            if (name!=null && name!="" && name!="null") {
                $.ajax ({
                    data: {temp_text:temp_text, name:name, subject_text:subject_text},
                    type: 'POST',
                    url:  'save_template.php',
                    success: function(response) {
                        if(response == "1") {
                            alert("template saved successfully");
                        } else {
                            alert(response);
                        }
                    }
                });
            } else {
                alert("invalid template name");
            }
            $.ajax ({
                url:  'update_templates.php',
                success: function(response) {
                    $("#container").html(response);
                }
            });
        });
        $(document).find("form").bind("submit", function(e){
            e.preventDefault();
            $.ajax({
                data: $(this).serialize(),
                type: $(this).attr('method'),
                url: $(this).attr('action'),
                success: function(response) {
                    alert(response);
                }
            });
        });
        $("#container").delegate("select", "change", function() {
            $("select option:selected").each(function () {
                $("#textarea").removeAttr("disabled");
                $("#send").removeAttr("disabled");
                $("#textarea").val($(this).val());
                $("#subject").val($(this).attr("subject"));
                $("#send").removeClass("disabledButton");
            });
        })
        .trigger('change');
    });
</script>
</html>